
<?php

$cmd=$_REQUEST['cmd'];
switch($cmd)
{
    
    case 1:
        
        break;
    case 2:
        addAdmin();
        
        break;
        
    case 3:
        break;
    case 4:
        editAdmin();
        
        break;
    case 5:
        deleteAdmin();
        break;
    case 6:
        search_admin();
        break;
    case 7:
        getAllAdmins();
        break;

}


function addAdmin(){
    if(isset($_REQUEST['fn'])){
            include_once('administrator.php');
            include_once 'users.php';
            
            $fname = $_REQUEST['fn'];
            $sname = $_REQUEST['sn'];
            $gender = $_REQUEST['gender'];
            $department = $_REQUEST['dpt'];
            $username = $_REQUEST['user'];
            $password = $_REQUEST['pass'];

            $obj = new administrator();
            $user = new users();
            $admin = 1;
            if(!$obj->add_admin($sname, $fname, $gender, $department)){
               echo '{"result":0,"message": "could not add admin"}';
            }
            else{
                $id = $obj->get_insert_id();
                $user->add_users($username, $password, $admin, $id);
                echo '{"result":1,"message": "added successfully","id":'.$id.'}';
            }
                
        }
}

function editAdmin(){
    if(isset($_REQUEST['fn'])){
            include_once("administrator.php");
           
            $fname = $_REQUEST['fn'];
            $sname = $_REQUEST['sn'];
            $gender = $_REQUEST['gender'];
            $department = intval($_REQUEST['dpt']);
            $nid = intval($_REQUEST['nid']);
            
           
            $obj = new administrator();
            if(!$obj->edit_administrator($nid, $sname, $fname, $gender, $department)){
               echo '{"result":0,"message": "update did not work."}';
            }
            else{
                echo '{"result":1,"message": "updated sucessfully "}';
            }
                
        }
}

function search_admin(){
	if(!isset($_REQUEST['st'])){
		//return error
		echo '{"result":0,"message": "search did not work."}';
	} 
        if(isset($_REQUEST['st'])){
            include_once('administrator.php');
            $obj1 = new administrator();
            
            $search_text = $_REQUEST['st'];
            if($obj1->search_admin_by_name($search_text)){
                $row=$obj1->fetch();
                echo '{"result":1,"administrators":[';	//start of json object
                while($row){
                        echo json_encode($row);			//convert the result array to json object
                        $row=$obj1->fetch();
                        if($row){
                                echo ",";					//if there are more rows, add comma 
                        }
                }
                echo "]}";
            }
            else{
		//return error
		echo '{"result":0,"message": "search did not work."}';
		return;
            } 
        }						
}

function getAllAdmins(){
            include_once('administrator.php');
            $obj1 = new administrator();
            $obj1->get_administrators();
            
            $row=$obj1->fetch();
                echo '{"result":1,"administrators":[';	//start of json object
                while($row){
                        echo json_encode($row);			//convert the result array to json object
                        $row=$obj1->fetch();
                        if($row){
                                echo ",";					//if there are more rows, add comma 
                        }
                }
            echo "]}";
}

function deleteAdmin(){
    include_once 'administrator.php';
        include_once 'users.php';
        if(isset( $_REQUEST["id"])){
            $nid = intval($_REQUEST['id']);
            
            $user = new users();
            $obj = new administrator();
            if(!$obj->delete_admin($nid)){
                echo '{"result":0,"message": "delete did not work."}';

            }
            else {
                $user->delete_user($nid);
                echo '{"result":1,"message": "deleted succesfully."}';

            }
        }
}
?>
